﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QuanLyThongTinNhanKhau.model.DAO
{
    class TamVangDAO
    {
        model.DAO.SQLServiceDAO service = new SQLServiceDAO();

        internal System.Data.DataTable loadDefault(string maNhanKhau)
        {
            String sql = "select * from NHANKHAU where MaNhanKhau = '" + maNhanKhau + "'";
            DataTable dt = service.getDataTable(sql);
            return dt;
        }

        internal DataTable loadTable(string maNhanKhau)
        {
            String sql = "select MaTamVang, t.MaNhanKhau, HoTen, GioiTinh, NgayTamVang, LyDoTamVang, Case When t.TinhTrang = 'Enable' then N'Mở' Else N'Đóng' End as TinhTrang from NHANKHAU n join TAMVANG t on n.MaNhanKhau = t.MaNhanKhau where t.MaNhanKhau = '"+maNhanKhau+"'";
            DataTable dt = service.getDataTable(sql);
            return dt;
        }

        internal bool isValidMaTamVang(string str)
        {
            String sql = "Select TOP(1) * from TamVang where MaTamVang = '" + str + "'";
            DataTable dt = service.getDataTable(sql);

            if (dt.Rows.Count != 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        internal bool themMoiTamVang(string maTamVang, string maNhanKhau, string ngayTamVang, string lyDoTamVang, string tinhTrang)
        {
            String sql = "insert into TamVang values ('" + maTamVang + "','" + maNhanKhau + "','" + ngayTamVang + "',N'" + lyDoTamVang + "', '" + tinhTrang + "')";
            return service.executeNonQuery(sql);
        }

        internal bool capNhatTamVang(string maTamVang, string maNhanKhau, string ngayTamVang, string lyDoTamVang, string tinhTrang)
        {
            String sql = "update TamVang set NgayTamVang = '" + ngayTamVang + "', LyDoTamVang = N'" + lyDoTamVang + "', TinhTrang = N'" + tinhTrang + "' where MaTamVang = N'" + maTamVang + "' and MaNhanKhau = N'" + maNhanKhau + "'";
            return service.executeNonQuery(sql);
        }

        internal bool deleteRow(string maTamVang, string maNhanKhau)
        {
            String sql = "delete from TamVang where MaTamVang = N'" + maTamVang + "' and MaNhanKhau = N'" + maNhanKhau + "'";
            return service.executeNonQuery(sql);
        }
    }
}
